/*
* AirService.java
* Created on  202015/6/29 9:25
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/29       longshihui    初始版本
*
*/

package com.infitecs.eyas.enthalpy.service;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.enthalpy.dao.AirDao;
import com.infitecs.eyas.enthalpy.model.Air;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.eyas.util.Parameter;
import com.infitecs.eyas.util.ParametersUtil;
import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Service
public class AirService {
    @Autowired
    private AirDao airDao;
    @Autowired
    private ExpressionDao expressionDao;

    public void updateGx(Param param){
        airDao.updateGx(param);
    }

    public Air getAir(int caseId){
        return airDao.getAir(caseId);
    }

    public void addAir(int caseId){
        airDao.addAir(caseId);
    }

    public Double calG8(Param param){
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.AIR_G8.toString());
        Air air = airDao.getAir(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("G1",air.getG1()));
            params.add(new Parameter("G2",air.getG2()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateGx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }

    public Double calG9(Param param){
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.AIR_G9.toString());
        Air air = airDao.getAir(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("G2",air.getG2()));
            params.add(new Parameter("G4",air.getG4()));
            params.add(new Parameter("G5",air.getG5()));
            params.add(new Parameter("G6",air.getG6()));
            params.add(new Parameter("G7",air.getG7()));
            params.add(new Parameter("G8",air.getG8()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateGx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }
}
